home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / 1987 / 12 / port / popwin.i < prev   
Text File  |  1987-12-21  |  6KB  |  144 lines

  1. /* POPWIN.I: Routines for pop-up windows                           */
  2. /* Externally defined are gotoxy(), wrtcha(), window(), chattr(),  */
  3. /*        and videomode().                                         */
  4. /* Notes: Programmer is responsible for assuring that the pop-up   */
  5. /*          defined in the POPDESCR structure is large enough to   */
  6. /*          contain the text.                                      */
  7. /*        Save your cursor position before calling these routines. */
  8. /*          They don't preserve or restore caller's cursor.        */
  9. /*        Unlike menubar, the POPDESCR structure doesn't contain a */
  10. /*          pointer to the text elements. Thus, you can use the    */
  11. /*          routines to create dialog boxes and help windows.      */
  12. /* --------------------------------------------------------------- */
  13. typedef struct {            /* window descriptor/control structure */
  14.   int   left, top, right, bottom;    /* window location, inclusive */
  15.   char  textAttr;                      /* text attribute in window */
  16.   int   border;                /* 0 = none, 1 = single, 2 = double */
  17. } POPDESCR;
  18.  
  19. static bord[][6] = {                          /* border characters */
  20.            { 196, 179, 218, 191, 217, 192 },
  21.            { 205, 186, 201, 187, 188, 200 }
  22. };
  23.  
  24. void popMake (POPDESCR *win)               /* create pop-up window */
  25. /* Must initialize the POPDESCR structure before calling.          */
  26. /* Does not write text to the window, but only creates it. After   */
  27. /*   this fcn returns, you can write text using popPuts() and      */
  28. /*   control the cursor with popxy(), both given below.            */
  29. {
  30. int  x, y, style, a;
  31.  
  32.   a = win->textAttr;
  33.   window (win->left, win->top, win->right, win->bottom,
  34.           win->textAttr);                           /* open window */
  35.   if ((style = win->border-1) >= 0)       { /* draw border outside */
  36.     gotoxy (win->left-1, win->top-1, 0);
  37.     wrtcha (bord [style][2], a, 0);         /* corners: upper left */
  38.     gotoxy (win->right+1, win->top-1, 0);
  39.     wrtcha (bord [style][3], a, 0);                 /* upper right */
  40.     gotoxy (win->right+1, win->bottom+1, 0);
  41.     wrtcha (bord [style][4], a, 0);                 /* lower right */
  42.     gotoxy (win->left-1, win->bottom+1, 0);
  43.     wrtcha (bord [style][5], a, 0);                  /* lower left */
  44.     for (x = win->left; x <= win->right; x++) {     /* horizontals */
  45.       gotoxy (x, win->top-1, 0);
  46.       wrtcha (bord [style][0], a, 0);
  47.       gotoxy (x, win->bottom+1, 0);
  48.       wrtcha (bord [style][0], a, 0);
  49.     }
  50.     for (y = win->top; y <= win->bottom; y++) {       /* verticals */
  51.       gotoxy (win->left-1, y, 0);
  52.       wrtcha (bord [style][1], a, 0);
  53.       gotoxy (win->right+1, y, 0);
  54.       wrtcha (bord [style][1], a, 0);
  55.     }
  56.   }
  57.   gotoxy (win->left, win->top, 0);
  58. } /* ------------------------ */
  59. void popScroll (POPDESCR *win)           /* scroll pop up one line */
  60. {
  61.   winScroll (win->left, win->top, win->right, win->bottom,
  62.              win->textAttr);
  63.   gotoxy (wherex (0), wherey (0) - 1, 0);
  64. } /* ------------------------ */
  65. void popxy (int x, int y, POPDESCR *w)  /* gotoxy for popup window */
  66. /* Allows you to express text coordinates relative to upper left   */
  67. /* corner of the window in video page 0                            */
  68. {
  69. int  row, col;
  70.  
  71.   row = w->top + y;
  72.   col = w->left + x;
  73.   gotoxy (col, row, 0);
  74. } /* ------------------------ */
  75. void popPuts (int x, int y, char string[],      /* write string to */
  76.               POPDESCR *win)                   /* specified window */
  77. {
  78. int   len, ch, p;
  79.  
  80.   popxy (x, y, win);                  /* position cursor in window */
  81.   len = strlen (string);
  82.   for (ch = 0; ch < len; ch++) {
  83.     if (string [ch] == '\n') {                   /* handle newline */
  84.       x = 0;
  85.       ++y;
  86.       popxy (x, y, win);
  87.       if ((y + win->top) > win->bottom) {    /* scroll if required */
  88.         popScroll (win);
  89.         --y;
  90.       }
  91.     } else {
  92.         if ((x + win->left) > win->right) {      /* outside window */
  93.           x = 0;                                 /* so wrap cursor */
  94.           ++y;
  95.           popxy (x, y, win);
  96.           if ((y + win->top) > win->bottom){ /* scroll if required */
  97.             popScroll (win);
  98.             --y;
  99.           }
  100.         }
  101.         wrtcha (string [ch], win->textAttr, 0); /* write next char */
  102.         ++x;
  103.         popxy (x, y, win);                       /* advance cursor */
  104.     }
  105.   }
  106. } /* ------------------------ */
  107. char *saveScrn (void)                        /* saves screen image */
  108. /* Call this routine before popMake(). It saves the current screen */
  109. /*  image at a location pointed to by the returned value. You must */
  110. /*  pass the same pointer back to restScrn() later in order to     */
  111. /*  make the pop-up go away.                                       */
  112. {
  113. int      c;
  114. unsigned srcSeg;
  115. char     *saveArea;
  116.  
  117.   saveArea = (char *) malloc (4096);             /* allocate space */
  118.   if (videomode (&c) == 7)
  119.     srcSeg = 0xB000;                         /* &monochrome buffer */
  120.   else
  121.     srcSeg = 0xB800;                      /* &text graphics buffer */
  122.   movedata (srcSeg, 0, _SS, (unsigned) saveArea, 4096);    /* save */
  123.   return (saveArea);                             /* return pointer */
  124. } /* ------------------------ */
  125. void restScrn (char *saveArea)            /* restores screen image */
  126. /* Call this routine when you want the pop up window to go away.   */
  127. /* It restores the screen to its appearance before the window. It  */
  128. /*   does NOT restore the cursor. That is your responsibility.     */
  129. /* This routine does not worry about snow on IBM's poorly designed */
  130. /*   CGA board. Snow may result when restoring the screen.         */
  131. {
  132. int      c;
  133. unsigned destSeg;
  134.  
  135.   if (videomode (&c) == 7)
  136.     destSeg = 0xB000;                        /* &monochrome buffer */
  137.   else
  138.     destSeg = 0xB800;                      /* text graphics buffer */
  139.   movedata (_SS,(unsigned) saveArea, destSeg, 0, 4096); /* restore */
  140.   free (saveArea);                            /* de-allocate space */
  141. } /* ------------------------ */
  142.  
  143.  
  144.